package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 不生成set，get方法，在类上添加lombok注解，例如@Data
 *
 * @ClassName: LombokPlugin
 * @Description:
 * @author: XO
 * @date: 2018年10月27日 上午10:27:13
 */
public class LombokPlugin extends PluginAdapter {
    //public class LombokPlugin extends DefaultCommentGenerator {

    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // 添加domain的import
        topLevelClass.addImportedType("lombok.Data");
        // topLevelClass.addImportedType("lombok.Builder");
        topLevelClass.addImportedType("lombok.NoArgsConstructor");
        topLevelClass.addImportedType("lombok.AllArgsConstructor");
        topLevelClass.addImportedType("com.baomidou.mybatisplus.annotation.TableField");
        topLevelClass.addImportedType("com.baomidou.mybatisplus.annotation.TableName");
        // 添加domain的注解(提供类所有属性的 getting 和 setting 方法)
        topLevelClass.addAnnotation("@Data");
        // mybatisplus注解,不需要的时候记得注释
        topLevelClass.addAnnotation("@TableName(\""+introspectedTable.getFullyQualifiedTable().getIntrospectedTableName()+"\")");
        // topLevelClass.addAnnotation("@Builder");
        // 提供无参构造
        topLevelClass.addAnnotation("@NoArgsConstructor");
        // 提供全参构造
        topLevelClass.addAnnotation("@AllArgsConstructor");
        // 添加domain的注释
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine("* @ClassName: " + topLevelClass.getType().getShortName());
        topLevelClass.addJavaDocLine("* @Description: ");
        topLevelClass.addJavaDocLine("* @author: Mybatis Generator");
        topLevelClass.addJavaDocLine("* @date " + date2Str(new Date()));
        topLevelClass.addJavaDocLine("*/");
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // Mapper文件的注释
        interfaze.addJavaDocLine("/**");
        interfaze.addJavaDocLine("* @ClassName: " + interfaze.getType().getShortName());
        interfaze.addJavaDocLine("* @Description: ");
        interfaze.addJavaDocLine("* @author: Mybatis Generator");
        interfaze.addJavaDocLine("* @date " + date2Str(new Date()));
        interfaze.addJavaDocLine("*/");
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成setter
        return false;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成getter
        return false;
    }

    /**
     * 格式化时间字符串
     *
     * @param date
     * @return
     * @author: renkai721
     * @date: 2018年7月13日 上午11:17:52
     * @Title: date2Str
     * @Description:
     */
    private String date2Str(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 ahh:mm:ss");
        return sdf.format(date);
    }

    /**
     * 字段调整(给实体字段添加数据库注释)
     * @param field
     * @param topLevelClass
     * @param introspectedColumn
     * @param introspectedTable
     * @param modelClassType
     * @return
     */
    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        field.addJavaDocLine("/**");
        field.addJavaDocLine("* " + introspectedColumn.getRemarks());
        field.addJavaDocLine("*/");
        // mybatisplus注解,不需要的时候记得注释
        field.addAnnotation("@TableField(\""+introspectedColumn.getActualColumnName()+"\")");
        return true;
    }
}
